package com.zhouchb.test.设计模式.单例模式;

/**
 * @Create 2023-06-24 21:17
 * @Description
 */
//饿汉式
public class SingleTon {
    private SingleTon (){}

    private final static  SingleTon instance  = new SingleTon();
    public static SingleTon getInstance(){
        return instance;
    }
}
class test{
    public static void main(String[] args) {
        SingleTon instance = SingleTon.getInstance();
        SingleTon instance1 = SingleTon.getInstance();
        System.out.println(instance1);
        System.out.println(instance);

        LanHan instance2 = LanHan.getInstance();
        LanHan instance3 = LanHan.getInstance();
        System.out.println(instance2);
        System.out.println(instance3);
    }

}

//懒汉式
 class LanHan{
    private LanHan(){}
    private  static volatile  LanHan instance ;

    public static LanHan getInstance(){
            if (instance == null){
                synchronized (LanHan.class){
                    if (instance==null){
                        instance = new LanHan();
                    }
                }
            }
            return instance;
    }
}